package com.lianboke.link.ad.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.lianboke.link.ad.R;
import com.lianboke.link.ad.utils.KLog;
import com.lianboke.link.ad.utils.imageloader.GlideLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片适配器
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {

    private Context mContext;
    private List<String> mImages;
    private LayoutInflater mInflater;
    private boolean isShowClosed=false;//是否显示删除按钮

    private OnItemClickListener mOnItemClickListener = null;

    public ImageAdapter(Context context) {
        mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
    }
    public void setmOnItemClickListener(OnItemClickListener onItemClickListener){
        mOnItemClickListener =   onItemClickListener;
    }
    public void setIsShowClosed(boolean showClosed){
        isShowClosed = showClosed;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_image, parent, false);
        return new ViewHolder(view);
    }

    //将数据与界面进行绑定的操作
    //payload用于item notify的时候只更新局部的view
    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
         String image = mImages.get(position).trim();
        KLog.e("img_tag_log","--------onBindViewHolder------------------:"+image);
        Glide.with(mContext).load(image).into(holder.ivImage);
        holder.ivClosed.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v,position);
            }
        });
        if(isShowClosed){
            holder.ivClosed.setVisibility(View.VISIBLE);
        }else{
            holder.ivClosed.setVisibility(View.GONE);
        }
      //  GlideLoader.loadImage(image,holder.ivImage);
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return mImages == null ? 0 : mImages.size();
    }

    public void refresh(List<String> images) {
        if(mImages==null) {
            mImages = new ArrayList<String>();
        }else{
            mImages.clear();
        }
        mImages.addAll(images);
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivImage;
        ImageView ivClosed;//关闭

        public ViewHolder(View itemView) {
            super(itemView);
            ivImage = (ImageView) itemView.findViewById(R.id.iv_image);
            ivClosed = (ImageView)itemView.findViewById(R.id.iv_closed);
        }
    }

    //监听器
    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }
}
